package com.wq.babysecretary.base;

import android.Manifest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wq.babysecretary.R;
import com.wq.babysecretary.weight.runtimepermissions.StatusBarCompat;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public  class UeBaseActivity extends AppCompatActivity {

    public Context context;
    private boolean mNowMode;
    private Unbinder unbinder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        context=this;
        setContentView(R.layout.baselayout);
        StatusBarCompat.compat(this, Color.parseColor("#FE7AA2"));
        unbinder = ButterKnife.bind(this);
        registerBoradcastReceiver();
        processLogic();
    }


    public void baseSetContentView(int layoutResId) {
        LinearLayout llContent = (LinearLayout) findViewById(R.id.content);
        LayoutParams params = new LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(layoutResId, null);
        llContent.addView(v, params);
        ButterKnife.bind(this);
    }

    public ImageView getGoBackBtn(){
        return (ImageView)findViewById(R.id.back);
    }
    public ImageView goSearch(){
        return (ImageView)findViewById(R.id.searchBtn);
    }
    public TextView getTitleView(){
        return (TextView)findViewById(R.id.title);
    }
    public void setTitleText(String title){
        ((TextView)findViewById(R.id.title)).setText(title);
    }
    public void showGoBackBtn(){
        getGoBackBtn().setVisibility(View.VISIBLE);
        getGoBackBtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UeBaseActivity.this.finish();
            }
        });
    }

    public void hiddenGoBackBtn(){
        getGoBackBtn().setVisibility(View.GONE);
    }
    public void showHeader(){
        ((LinearLayout)findViewById(R.id.title_bar)).setVisibility(View.VISIBLE);
    }

    public void hiddenHeader(){
        ((LinearLayout)findViewById(R.id.title_bar)).setVisibility(View.GONE);
    }

    public void showFooter(){
        ((LinearLayout)findViewById(R.id.footerbar)).setVisibility(View.VISIBLE);
    }

    public void hiddenFooter(){
        ((LinearLayout)findViewById(R.id.footerbar)).setVisibility(View.GONE);
    }

    public BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("com.wq.babysecretary".equals(intent.getAction())) {
//                Intent in=new Intent(UeBaseActivity.this, LoginActivity.class);
//                startActivity(in);
//                finish();
            }
        }
    };

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("com.wq.babysecretary");
        // 注册广播
        registerReceiver(mFinishReceiver, myIntentFilter);
    }

    public void unregisterBoradcastReceiver() {
        unregisterReceiver(mFinishReceiver);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregisterBoradcastReceiver();
        unbinder.unbind();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }
    /**
     * 逻辑使用区
     */
    protected void processLogic(){
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
    }


    /**
     * 对各种控件进行设置、适配、填充数据
     */

    protected void gone(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        }
    }

    protected void visible(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.VISIBLE);
                }
            }
        }

    }
    protected boolean isVisible(View view) {
        return view.getVisibility() == View.VISIBLE;
    }
    private void checkSelfPermission(){
        ActivityCompat.requestPermissions(((Activity) this),
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_SETTINGS},
                1);
    }
}

